//
//  WBWelcomView.swift
//  Weibo
//
//  Created by yinbo on 2018/3/15.
//  Copyright © 2018年 yinbo. All rights reserved.
//

import UIKit

class WBWelcomView: UIView {
    
    
    @IBOutlet weak var iconView:UIImageView!
    @IBOutlet weak var tipLabel:UILabel!
    @IBOutlet weak var bottomCons:NSLayoutConstraint!
    class func welcomeView()->WBWelcomView{
        let nib = UINib(nibName:"WBWelcomView",bundle:nil)
        let v:WBWelcomView = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomView
        v.frame = UIScreen.main.bounds
        return v
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.blue
    }
//    override func awakeFromNib() {
//        super.awakeFromNib()
//    }
    required init?(coder aDecoder: NSCoder) {
        //initwithcoder只是刚刚从XIB的二进制文件将视图数据加载完成
        //还没有喝代码连线建立起关系，所以开发时，千万不要在这个方法中处理UI
        super.init(coder: aDecoder)
        
//        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        //设置头像
        guard let urlString = WBNetworkManager.shared.userAccount.avatar_large,let url = URL(string: urlString) else{
            return
        }
        iconView.sd_setImage(with:url, placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
    }
    //视图被添加到window上，表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        //视图是使用自动布局来设置的，只是设置了约束
        //当视图被添加到窗口上市，根据父视图的大小，计算约束值，更新控件位置
        self.layoutIfNeeded()
        bottomCons.constant = bounds.size.height-200
        //如果控件们的frame还没有计算好！所有的约束会一起动画
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            //更新约束
            self.layoutIfNeeded()
            }, completion: { (_) in
                
                UIView.animate(withDuration: 1.0, animations: { 
                    self.tipLabel.alpha = 1
                    }, completion: { (_) in
                    self.removeFromSuperview()
                })
            
        })
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
